package com.asktgapp.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.util.Log;
import android.widget.ImageView;

import com.asktgapp.R;
import com.asktgapp.application.ApiConfig;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.BitmapImageViewTarget;

/**
 * @author Lee
 * @Title: {图片加载类 使用开源框架 image-loader}
 * @Description:{描述}
 * @date 2016/4/12
 */
public class ImageDisplayUtil {

    /**
     * 个人中心头像
     **/
    public static final int DISPLAY_PERSONAL_HEADER = 0;
    /**
     * 头像
     **/
    public static final int DISPLAY_HEADER = 1;
    /**
     * 圆形图片
     **/
    public static final int DISPLAY_ROUND_IMAGE = 2;
    /**
     * 小图片  30*30 或更小
     **/
    public static final int DISPLAY_SMALL_IMAGE = 3;
    /**
     * 普通大小图片  120*120 左右
     **/
    public static final int DISPLAY_COMMON_IMAGE = 4;
    /**
     * 大长图 Banner、宽度占满屏幕的图片等
     **/
    public static final int DISPLAY_BIG_IMAGE = 5;

    /**
     * 图片种类，用于加载图片是url拼接
     */
    public static final String IMG_SOURCE = "source";
    public static final String IMG_COMPRESS = "compress";
    public static final String IMG_THUMBNAIL = "thumbnail";

    public static void displayImage(Context context, String url, ImageView imageView) {
        Glide.with(context).load(url).into(imageView);
    }

    /**
     * 加载普通网络大图
     **/
    public static void LoadNetImage(ImageView imageView, Context context, String src) {

        try {
            Glide.with(context)
                    .load(src)
                    .placeholder(R.mipmap.default_item)
                    .crossFade()
                    .override(240, 240)
//                    .thumbnail(0.1f)
                    .error(R.mipmap.default_item)
                    .diskCacheStrategy( DiskCacheStrategy.NONE )
                    .into(imageView);
        } catch (Exception e) {

        }

    }
    /**
     * 加载文答
     **/
    public static void LoadASKIMG(ImageView imageView, Context context, String src) {

        try {
            Glide.with(context)
                    .load(src)
                    .crossFade()
                    .placeholder(R.mipmap.default_item)
                    .override(120, 120)
                    .thumbnail(0.1f)
//                    .error(R.mipmap.ic_launcher)
//                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .into(imageView);
        } catch (Exception e) {

        }

    }
    public static void LoadShopGoods(ImageView imageView, Context context, String src) {

        try {
            Glide.with(context)
                    .load(src)
                    .crossFade()
                    .placeholder(R.mipmap.home_shop_im1)
                    .override(120, 120)
                    .thumbnail(0.1f)
//                    .error(R.mipmap.ic_launcher)
//                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .into(imageView);
        } catch (Exception e) {

        }

    }
    /**
     * 加载普通网络大图
     **/
    public static void LoadNetImageWithUrl(ImageView imageView, Context context, String src) {
        try {
            Glide.with(context)
                    .load(src)
                    .crossFade()
                    .placeholder(R.mipmap.default_item)
//                    .override(240, 240)
//                    .thumbnail(0.1f)
                    .error(R.mipmap.default_item)
//                    .diskCacheStrategy( DiskCacheStrategy.NONE )
                    .into(imageView);
        } catch (Exception e) {

        }

    }

    /**
     * 加载普通网络大图(长)
     **/
    public static void LoadLongNetImage(ImageView imageView, Context context, String src) {
        try {
            Glide.with(context)
                    .load(src)
                    .crossFade()
                    .placeholder(R.mipmap.default_banner)
//                    .override(240, 240)
//                    .thumbnail(0.1f)
                    .error(R.mipmap.default_banner)
                    .into(imageView);
        } catch (Exception e) {

        }

    }

    /**
     * 设置头像时调用此方法
     **/
    public static void LoadNetThumbnailImage(ImageView imageView, Context context, String src, float thumbnail) {
        try {
            Glide.with(context)
                    .load(src)
                    .placeholder(R.mipmap.ic_launcher)
                    .crossFade()
                    .error(R.mipmap.ic_launcher)
                    .thumbnail(thumbnail)
                    .into(imageView);
        } catch (Exception e) {

        }

    }

    /**
     * 加载圆形图片
     **/
    public static void LoadCircleImg(final Context context, String src, final ImageView imageView) {
        Glide.with(context).load(src).asBitmap()
                .centerCrop()
                .placeholder(R.mipmap.default_head)
                .thumbnail(0.1f)
                .into(new BitmapImageViewTarget(imageView) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                        circularBitmapDrawable.setCircular(true);
                        imageView.setImageDrawable(circularBitmapDrawable);
                    }
                });
    }

    public static void LoadCircleImgOnlocal(final Context context, String src, final ImageView imageView) {
        Glide.with(context).load(src).asBitmap()
                .centerCrop()
                .placeholder(R.mipmap.default_head)
                .into(new BitmapImageViewTarget(imageView) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                        circularBitmapDrawable.setCircular(true);
                        imageView.setImageDrawable(circularBitmapDrawable);
                    }
                });
    }


    /**
     * 添加高斯模糊
     **/
    public static void LoadNetTransformatImage(ImageView imageView, Context context, String src) {
        try {
            Glide.with(context)
                    .load(ApiConfig.IMG_URL + src)
                    .placeholder(R.mipmap.ic_launcher)
                    .crossFade()
                    .error(R.mipmap.ic_launcher)
                    //  .bitmapTransform(new BlurTransformation(context))
                    .into(imageView);
        } catch (Exception e) {

        }

    }

    /**
     * 加载用户圆形的头像
     *
     * @param imageView
     * @param context
     * @param src
     */
    public static void loadCircleHeadImage(final Context context, final ImageView imageView, String src) {
        try {
            Glide.with(context).load(src).asBitmap()
                    .centerCrop()
                    .thumbnail(0.1f)
                    .placeholder(R.mipmap.default_head)
                    .error(R.mipmap.default_head).
                    into(new BitmapImageViewTarget(imageView) {
                        @Override
                        protected void setResource(Bitmap resource) {
                            RoundedBitmapDrawable circularBitmapDrawable =
                                    RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                            circularBitmapDrawable.setCircular(true);
                            imageView.setImageDrawable(circularBitmapDrawable);
                        }
                    });

        } catch (Exception e) {
            Log.e("imageDisplay", e.toString());
        }
    }

    public static void LoadDisImage(Context context, ImageView imageView, String src) {
        try {
            Glide.with(context)
                    .load(src)
                    .thumbnail(0.1f)
                    .placeholder(R.mipmap.default_item)
                    .crossFade()
                    .error(R.mipmap.default_item)
                    .into(imageView);
        } catch (Exception e) {

        }

    }


}
